package com.ainiyar.springweb.config.security.handler;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.pojo.entity.Admin;
import com.ainiyar.springweb.pojo.entity.Menu;
import com.ainiyar.springweb.pojo.admin.vo.RouterVo;
import com.ainiyar.springweb.util.JWTUtil;
import com.ainiyar.springweb.util.MenuTreeUtils;
import com.ainiyar.springweb.util.WebUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**************************************
 * File Name : LoginSuccessHandler
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/5  00:25
 * Desc :登录成功处理类
 ***************************************/
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
    @Resource
    private JWTUtil jwtUtil;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //设置响应编码格式
        response.setContentType("application/json;charset=utf-8");
        Admin admin = (Admin) authentication.getPrincipal();
        //生成token
        String token = jwtUtil.generateToken(admin);
        //token过期时间
        Long token_time = jwtUtil.extractTime(token);
        //登录成功后返回的数据
        Map<String, Object> loginResult = new HashMap<String, Object>();
        loginResult.put("account", admin.getAccount());
        loginResult.put("nickName", admin.getNickName());
        loginResult.put("token", token);
        loginResult.put("expireTime", token_time);
        loginResult.put("avatar",admin.getAvatar());
        loginResult.put("last_login","");
        loginResult.put("last_ip","");
        //获取该管理员拥有的权限信息
        List<Menu> permissionList = admin.getPermissionList();
        //筛选当前管理员拥有的目录、菜单数据
        List<Menu> collect = permissionList.stream()
                //只筛选出目录和菜单、按钮不需要添加到路由菜单中
                .filter(item -> item != null && item.getType() != 3)
                .collect(Collectors.toList());
        //生成路由数据
        List<RouterVo> router = MenuTreeUtils.makeRouter(collect, 0L);
        loginResult.put("menus", router);
        //处理按钮权限
        Collection<? extends GrantedAuthority> authorities = admin.getAuthorities();
        loginResult.put("authorities", authorities);
        ResultResponse<Object> result = ResultResponse.Result(loginResult, "登录成功");
        //登录成功后把相关的信息存放到redis中

        WebUtil.renderString(response, result);
    }
}

